//
//  HMEmoticonKeyboardCell.swift
//  表情键盘
//
//  Created by WGP on 16/4/20.
//  Copyright © 2016年 WGP. All rights reserved.
//

import UIKit

//1.定义代理协议
protocol HMEmoticonKeyboardCellDelegate: NSObjectProtocol
{
//    2.代理方法
    func emoticonKeyboardPageCell(cell: HMEmoticonKeyboardCell, didSelectedEmoticon: HMEmoticonModel)
    func emoticonKeyboardCellDidClickDeleteBtn(cell: HMEmoticonKeyboardCell)
}

class HMEmoticonKeyboardCell: UICollectionViewCell {
    
//    3.定义代理属性
    weak var delegate:HMEmoticonKeyboardCellDelegate?
    
//    cell要显示的pageEmoticon
    var pageEmoticon: [HMEmoticonModel]?
    {
        didSet
        {
//            比如表情最后一页只有2个表情,为防止复用,应该先隐藏,赋值才显示
            for button in emoticonButtons
            {
                button.hidden = true
            }
//            遍历模型把相对应的模型赋值给相应的按钮,按钮该怎么显示是按钮的事
            for (index,emoticonModel) in pageEmoticon!.enumerate()
            {
                let emoticonButton = emoticonButtons[index]
                emoticonButton.hidden = false
                
                emoticonButton.emoticonModel = emoticonModel
            }
         }
    }
    
    var indexPath: NSIndexPath?
    {
        didSet
        {
            debugLabel.text = "当前第\(indexPath!.section)组,\(indexPath!.item)页"
            recentLabel.hidden = indexPath?.section != 0
        }
    }
    
    ///存放20个表情按钮,方便反问20个表情按钮
    private var emoticonButtons: [HMEmoticonButton] = [HMEmoticonButton]()

    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        setupUI()
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI()
    {
        contentView.addSubview(debugLabel)
        debugLabel.frame = bounds
        
        contentView.addSubview(deleteButton)
        contentView.addSubview(recentLabel)
        
        createEmoticonsButton()
    }
    
    ///创建20个表情按钮
    private func createEmoticonsButton()
    {
        for _ in 0..<20
        {
            let button = HMEmoticonButton()
            button.titleLabel?.font = UIFont.systemFontOfSize(32)
            
            button.addTarget(self, action: Selector("didClickEmoticonButton:"), forControlEvents: .TouchUpInside)
            
            emoticonButtons.append(button)
            contentView.addSubview(button)
        }
    }
    
    @objc private func didClickEmoticonButton(button: HMEmoticonButton)
    {
        delegate?.emoticonKeyboardPageCell(self, didSelectedEmoticon: button.emoticonModel!)
    }
    @objc private func didClickDeleteButton()
    {
//        调用代理
        delegate?.emoticonKeyboardCellDidClickDeleteBtn(self)
    }
    
    private func layoutButtonFrame()
    {
        let marginLR: CGFloat = 5
        let marginH: CGFloat = 25
        
        let width = (self.frame.width-2 * marginLR)/CGFloat(HMEmoticonColumnOfPage)
        
        let height = (self.frame.height - marginH)/CGFloat(HMEmoticonRowOfPage)
        
//        emticonButtons.enumerate不仅会将按钮返回给我们,还会将按钮在数组中的位置也返回
        for (index,button) in emoticonButtons.enumerate()
        {
           let column = index % HMEmoticonColumnOfPage
           let row = index / HMEmoticonColumnOfPage
            
            let x = marginLR + CGFloat(column) * width
            let y = CGFloat(row) * height
            
            let frame = CGRect(x: x, y: y, width: width, height: height)
            button.frame = frame
        }
        
//        设置删除按钮的frame
        let buttonFrame = CGRect(x: marginLR + width*CGFloat(HMEmoticonColumnOfPage-1), y: height * CGFloat(HMEmoticonRowOfPage-1), width: width, height: height)
        deleteButton.frame = buttonFrame
        
        let x = (contentView.frame.width-recentLabel.frame.width)*0.5
        let y = contentView.frame.height - recentLabel.frame.height-5
        recentLabel.frame.origin = CGPoint(x: x, y: y)
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        layoutButtonFrame()
    }
    
//    MARK: - 懒加载
    private lazy var debugLabel: UILabel =
    {
       let label = UILabel()
        
        label.textColor = UIColor.redColor()
        label.font = UIFont.systemFontOfSize(20)
        
        return label
    }()
    
    ///删除Button
    private lazy var deleteButton: UIButton =
    {
       let button = UIButton()
        
        button.setImage(UIImage(named: "compose_emotion_delete"), forState: .Normal)
        button.setImage(UIImage(named: "compose_emotion_delete_highlighted"), forState: .Highlighted)
        
        button.addTarget(self, action: Selector("didClickDeleteButton"), forControlEvents: .TouchUpInside)
        
        return button
    }()
    
    ///最近使用label
    private lazy var recentLabel: UILabel =
    {
       let label = UILabel()
        
        label.textColor = UIColor.lightGrayColor()
        label.font = UIFont.systemFontOfSize(12)
        
        label.text = "最近使用表情"
        label.sizeToFit()
        
        return label
    }()
    
}
